from django.conf import settings
from django.shortcuts import redirect
from rest_framework.response import Response
from rest_framework.views import APIView

from utils.fastdfs.storage import FDFSStorage


# Create your views here.
class UploadAvatarView(APIView):
    '''
    上传头像
    '''

    def post(self, request):
        storage = FDFSStorage()
        ret = storage._save('', request.data.get('img'))
        img_url = settings.FDFS_URL + ret;
        return Response({'imgurl': img_url})


class UploadCommonView(APIView):
    """
    富文本编辑器
    """

    def post(self, request):
        storage = FDFSStorage()
        ret = storage._save('', request.data.get('upload'))
        from_address = request.META['HTTP_REFERER']
        from_address = from_address[:from_address.rfind('/'):]
        ret_url = from_address + '/upload_success.html?image_url=' + settings.FDFS_URL + ret + '&CKEditorFuncNum=' + request.query_params.get(
            'CKEditorFuncNum')
        return redirect(ret_url)
